package dth.dao;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import dth.entity.Account;
import dth.entity.Dealer;
import dth.entity.Distributor;
import dth.entity.Faq;
import dth.entity.User;
import dth.utils.DthUtil;

public class AccountDAO {
	private SessionFactory sessionFactory = new AnnotationConfiguration()
			.configure().buildSessionFactory();
	private Session session = sessionFactory.openSession();

	public Account checkLogin(String userName, String pass) {
		String password = DthUtil.convertToMD5(pass);
		if (userName == null || password == null || userName.length() == 0
				|| password.length() == 0)
			return null;
		String queryString = "select * from account where user_name=:username and password=:pass";
		Query query = session.createSQLQuery(queryString).addEntity(
				Account.class);
		query.setParameter("username", userName);
		query.setParameter("pass", password);
		List<Object> list = query.list();
		if (list != null && list.size() > 0)
			return (Account) list.get(0);
		else
			return null;
	}

	public java.util.List<Account> getListAccount() {
		//session.beginTransaction();
		String queryString = "select * from account";
		Query query = session.createSQLQuery(queryString).addEntity(
				Account.class);
		
		return query.list();
	}

	public int insert(Account account) {
		Transaction tx = session.beginTransaction();
		String queryString = "INSERT into account VALUES(0,:username,:pass,:fkDealerId,:fkDistributorId,:fkUserId,:fkAdminId,:dateInsert)";
		System.out.println("-----INSERT into account VALUES(0,"
				+ account.getUserName() + "," + account.getUserPass() + ","
				+ account.getFkDealerId() + "," + account.getFkDistributorId()
				+ "," + account.getFkUserId() + "," + account.getFkAdminId()
				+ "," + account.getDateInsert());
		Query query = session.createSQLQuery(queryString);
		query.setParameter("username", account.getUserName());
		query.setParameter("pass", account.getUserPass());
		query.setParameter("fkDealerId", account.getFkDealerId());
		query.setParameter("fkDistributorId", account.getFkDistributorId());
		query.setParameter("fkUserId", account.getFkUserId());
		query.setParameter("fkAdminId", account.getFkAdminId());
		query.setParameter("dateInsert", account.getDateInsert());
		int count = query.executeUpdate();
		tx.commit();
		return count;
	}

	public int update(Account account) {
		Transaction tx = session.beginTransaction();
		session.update(account);
		tx.commit();
		return 1;
	}

	public int delete(int id) {
		Transaction tx = session.beginTransaction();
		String queryString = "delete from account where user_id= :id";
		Query query = session.createSQLQuery(queryString)
				.setParameter("id", id);
		int count = query.executeUpdate();
		tx.commit();
		return count;
	}

	public boolean checkPasswordIsValid(String username, String password) {
		Transaction tx = session.beginTransaction();
		String queryString = "select * from account where username= :username";
		Query query = session.createSQLQuery(queryString).addEntity(
				Account.class);
		query.setParameter("username", username);
		List<Account> list = query.list();
		for (Account temp : list) {
			if (temp.getUserPass().equals(password)) {
				return true;
			}
		}
		return false;
	}

	/*
	 * check user name invaild
	 */
	public boolean checkUserNanmeIsVaild(String userName) {
		String queryString = "select * from account where user_name=:name";
		Query query = session.createSQLQuery(queryString).addEntity(
				Account.class);
		query.setParameter("name", userName);
		List list = query.list();
		if (list == null || list.size() == 0)
			return true;
		else
			return false;
	}

	public static List<SelectItem> getListUserCmbx(){
		UserDAO dao=new UserDAO();
		List<User> list=dao.getListUser();
		List<SelectItem> listSelectItems=new ArrayList<SelectItem>();
		if(list==null || list.size()==0)
			return listSelectItems;
		else{
			for(User user:list){
				SelectItem item=new SelectItem(user.getCustomerId(),user.getCustomerName());
				listSelectItems.add(item);
			}
			System.out.println("---cmbx user:"+listSelectItems.size());
			return listSelectItems;	
		}
	}
	public static List<SelectItem> getListDealerCmbx(){
		DealerDAO dao=new DealerDAO();
		List<Dealer> list=dao.getListDealer();
		List<SelectItem> listSelectItems=new ArrayList<SelectItem>();
		if(list==null || list.size()==0)
			return listSelectItems;
		else{
			for(Dealer dealer:list){
				SelectItem item=new SelectItem(dealer.getDealerId(),dealer.getDealerName());
				listSelectItems.add(item);
			}
			System.out.println("----cmbx dealer size:"+listSelectItems.size());
			return listSelectItems;	
		}
	}
	public static List<SelectItem> getListDistributorCmbx(){
		DistributorDAO dao=new DistributorDAO();
		List<Distributor> list=dao.getListDistributor();
		List<SelectItem> listSelectItems=new ArrayList<SelectItem>();
		if(list==null || list.size()==0)
			return listSelectItems;
		else{
			for(Distributor  distributor:list){
				SelectItem item=new SelectItem(distributor.getDistributorId(),distributor.getDistributorName());
				listSelectItems.add(item);
			}
			System.out.println("---cmbx size:"+listSelectItems.size());
			return listSelectItems;	
		}
	}
}
